package com.first.computerstore.controller;

import com.first.computerstore.entity.Address;
import com.first.computerstore.service.AddressService;
import com.first.computerstore.utils.JsonResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 地址
 */
@RestController
@RequestMapping("/address")
public class AddressController extends BaseController{

    @Resource
    private AddressService addressService;

    /**
     * 根据用户id查询用户的地址列表
     * @param session
     * @return
     */
    @PostMapping("/userAddressList")
    public JsonResult<List<Address>> queryAddressListByUid(HttpSession session){
//        获取uid
        Integer uid = getUserIdFromSession(session);

        List<Address> addressList = addressService.queryAddressListByUid(uid);

        return new JsonResult<>(200,addressList);
    }
/**
 *
     * 新增地址
     * @param address
     * @param request
     * @return
     */
    @PostMapping
    public JsonResult<Address> addAddress(Address address, HttpServletRequest request){
        Integer uid = (Integer) request.getSession().getAttribute("uid");
         addressService.addAddress(uid,address);
        return new JsonResult<>(OK);
    }

    /**
     * 地址显示
     * @param request
     * @return
     */
    @GetMapping
    public JsonResult<List<Address>> ShowAddress(HttpServletRequest request){
        Integer uid = (Integer) request.getSession().getAttribute("uid");
        List<Address> addressList = addressService.ShowAddress(uid);
        return new JsonResult<>(OK,addressList);
    }

    /**
     *设置默认地址
     * @return
     */
    @PostMapping("setAddress")
    public JsonResult<Address> setAddressStatus(String aid,HttpServletRequest request){
        Integer uid = (Integer) request.getSession().getAttribute("uid");
        addressService.setAddressStatus(uid, Integer.valueOf(aid));
        return new JsonResult<>(OK);
    }

    /**
     * 点击修改页面的数据回显
     * @param aid
     * @return
     */
    @GetMapping("queryOneAddress")
    public JsonResult<Address> queryOneAddress(String aid){
        Address address = addressService.queryOneAddress(aid);
        return new JsonResult<>(OK,address);
    }

    /**
     * 更新地址
     * @param address
     * @return
     */
    @PostMapping("updateAddress")
    public JsonResult<Address> updateAddress(Address address){
        addressService.updateAddress(address);
        return new JsonResult<>(OK);
    }

    /**
     * 删除地址
     * @param aid
     * @return
     */
    @PostMapping("deleteAddress")
    public JsonResult<Address> deleteAddress(String aid){
        addressService.deleteAddress(aid);
        return new JsonResult<>(OK);
    }
}
